/**
 * JAVACC DEMO 1.0
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.SystemTools;
import com.apache.database.constant.Validator;
import com.apache.rpc.common.LoadRpcService;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.uct.common.ToolsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * description:  根据用户token获取用户对象
 *
 * @author iussoft 创建时间：2016-3-30
 */
public class SsoUserInfoPlugin implements PluginConnector {

    private Logger log = LoggerFactory.getLogger(SsoUserInfoPlugin.class);

    private ProxyManager uctProxyManager;

    /**
     * TODO 根据用户token获取用户对象.  <br>
     * 参数说明:<br>
     * token:token串;<br>
     * userEname:用户账号;
     */
    public Object execute(ParamsVo vo) throws Exception {
        String userEname = String.valueOf(vo.getParams("userEname"));
        String token = Validator.getDefaultStr(String.valueOf(vo.getParams("token")), "");
        ResultEntity result = new ResultEntity();
        result.setResult("true");
        try {
            if (Validator.isNull(userEname)) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("tokenId", token);
                String sysEname = SystemTools.getInstance().getValue("sysEname");
                map.put("sysEname", sysEname);
                log.info("map=" + map);
                ResultEntity entity = LoadRpcService.service()
                        .doServiceClient("ssoService", "checkToken", map, getInterfaceRegister());
                log.info("entity=" + entity);
                String tokenId = String.valueOf(entity.getEntity());
                if (Validator.isNotNull(tokenId) && token.equals(tokenId)) {
                    userEname = Validator.getDefaultStr(userEname, entity.getMessage());
                } else {
                    result.setResult("false");
                    result.setMessage(entity.getMessage());
                    return result;
                }
            }
            ParamsVo pvo = new ParamsVo();
            pvo.setParams("userEname", userEname);
            pvo.setKey("loginUser");
            Object obj = uctProxyManager.doInvoke(pvo);
            result.setEntity(obj);
            result.setMessage("获取用户信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setResult("false");
            result.setMessage("发生未知异常！");
        }
        return result;
    }

    public void setUctProxyManager(ProxyManager uctProxyManager) {
        this.uctProxyManager = uctProxyManager;
    }

    private InterfaceRegister getInterfaceRegister() {
        InterfaceRegister register = new InterfaceRegister();
        String type = ToolsUtil.getInstance().getValueByKey("req_type");
        if ("socket".equals(type)) {
            String ip = ToolsUtil.getInstance().getValueByKey("check_url");
            String port = ToolsUtil.getInstance().getValueByKey("check_port");
            register.setAddress(ip);
            register.setPort(port);
            register.setCallType("socket");
        } else {
            String ssoPath = ToolsUtil.getInstance().getValueByKey("req_url");
            register.setAddress(ssoPath);
            if (ssoPath.startsWith("https:"))
                register.setCallType("https");
            else {
                register.setCallType("http");
            }
        }
        return register;
    }
}
